package com.ruoyi.web.controller.system;

import java.io.File;
import java.util.List;

import com.google.zxing.WriterException;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.code.QRcodeImageUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TmcVisitorManage;
import com.ruoyi.system.service.ITmcVisitorManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.Code.drawLogoQRCode;

/**
 * 访客记录Controller
 * 
 * @author ruoyi
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/system/manage")
public class TmcVisitorManageController extends BaseController
{
    @Autowired
    private ITmcVisitorManageService tmcVisitorManageService;

    /**
     * 查询访客记录列表
     */
    //@PreAuthorize("@ss.hasPermi('system:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(TmcVisitorManage tmcVisitorManage)
    {
        startPage();
        List<TmcVisitorManage> list = tmcVisitorManageService.selectTmcVisitorManageList(tmcVisitorManage);
        return getDataTable(list);
    }

    @GetMapping("/queryVisitorManagelist")
    public TableDataInfo queryVisitorManagelist(Long publicuserId)
    {
        List<TmcVisitorManage> list = tmcVisitorManageService.queryVisitorManagelist(publicuserId);
        return getDataTable(list);
    }

    /**
     * 导出访客记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:manage:export')")
    @Log(title = "访客记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TmcVisitorManage tmcVisitorManage)
    {
        List<TmcVisitorManage> list = tmcVisitorManageService.selectTmcVisitorManageList(tmcVisitorManage);
        ExcelUtil<TmcVisitorManage> util = new ExcelUtil<TmcVisitorManage>(TmcVisitorManage.class);
        return util.exportExcel(list, "manage");
    }

    /**
     * 获取访客记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:manage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tmcVisitorManageService.selectTmcVisitorManageById(id));
    }

    @GetMapping("/visitqrcode")
    public TmcVisitorManage VisitQrCode(@RequestParam(value = "visitqrcode") String visitqrcode)
    {
        return tmcVisitorManageService.selectvisitqrcodeById(visitqrcode);
    }

    @GetMapping("/code")
    public AjaxResult code(Long id){
        String filepath = RuoYiConfig.getProfile()+"//fk//"+id;
        String filename = id+"";
        //找code
        TmcVisitorManage tmcVisitorManage = tmcVisitorManageService.selectTmcVisitorManageById(id);
        drawLogoQRCode(filepath, filename, tmcVisitorManage.getVisitqrcode());
        return AjaxResult.success("图片上传成功","profile/fk/" +id+ "/" + filename + ".png");
    }
    /**
     * 新增访客记录
     */
    //@PreAuthorize("@ss.hasPermi('system:manage:add')")
    @Log(title = "访客记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmcVisitorManage tmcVisitorManage) throws Exception {
        return AjaxResult.success("生成访客记录成功",tmcVisitorManageService.insertTmcVisitorManage(tmcVisitorManage));
    }

    /**
     * 修改访客记录
     */
    //@PreAuthorize("@ss.hasPermi('system:manage:edit')")
    @Log(title = "访客记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmcVisitorManage tmcVisitorManage)
    {
        return toAjax(tmcVisitorManageService.updateTmcVisitorManage(tmcVisitorManage));
    }
//修改状态
    @GetMapping("/editState")
    public AjaxResult editState(@RequestParam(value = "id") Long id)
    {
        TmcVisitorManage tmcVisitorManage = new TmcVisitorManage();
        tmcVisitorManage.setId(id);
        tmcVisitorManage.setState(1L);
        return toAjax(tmcVisitorManageService.updateTmcVisitorManage(tmcVisitorManage));
    }
    //修改状态
    @GetMapping("/editStatecancel")
    public AjaxResult editStatecancel(@RequestParam(value = "id") Long id)
    {
        TmcVisitorManage tmcVisitorManage = new TmcVisitorManage();
        tmcVisitorManage.setId(id);
        tmcVisitorManage.setState(3L);
        return toAjax(tmcVisitorManageService.updateTmcVisitorManage(tmcVisitorManage));
    }
    /**
     * 删除访客记录
     */
    @PreAuthorize("@ss.hasPermi('system:manage:remove')")
    @Log(title = "访客记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tmcVisitorManageService.deleteTmcVisitorManageByIds(ids));
    }
}
